/**
 * Created by Administrator on 2017/6/1.
 */
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
  state: {
    userlist: []
  },
  mutations: {
    init (state) {
      for (let i = 0; i < 100; i++) {
        let user = {
          id: i,
          name: '王' + i,
          age: parseInt(Math.random() * (80 - 20 + 1) + 20, 10)
        }
        state.userlist.push(user)
      }
    }
  },
  actions: {
    getUserById ({commit, state}, id) {
      return new Promise((resolve, reject) => {
        var obj = state.userlist.find(function (obj) {
          return obj.id === id
        })
        resolve(obj)
      })
    },
    addUser ({commit, state}, user) {
      return new Promise((resolve, reject) => {
        user.id = state.userlist.length
        state.userlist.push(user)
        resolve()
      })
    }
  }
})
export default store
